﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Client.Service;
using ModelLayer;
using System.Collections;


namespace Client
{

    public partial class SeatReservation : Form
    {
        private IShow showService;
        private ISeat seatService;
        public int adultValue { get; set; }
        public int childValue { get; set; }
        public int seniorValue { get; set; }
        public int total { get; set; }
        public double test { get; set; }
        public int totalseat { get; set; }
        public int itotalseat { get; set; }
        public bool found = false;
        private const int TEMP = 1;

        Dictionary<Button, Seat> seats;
        private List<Seat> list;
        private List<Seat> booked_seats;

        public SeatReservation()
        {
            
            InitializeComponent();
            showService = new ShowClient("BasicHttpBinding_IShow");
            seatService = new SeatClient("BasicHttpBinding_ISeat");
            ComboInformation();
            populateCombobox();
            list = seatService.GetAllSeatsOnShow(5);
            seats = new Dictionary<Button, Seat>();
            MakeButtons();
            date();
            time();
            SeatAlgorithm();
        }

        public void ComboInformation()
        {

            Show show = showService.GetShow(5);

            //List<Show> shows = new List<Show>(showService.GetAllShows());
            lblViewShow.Text = show.Type.Title.ToString();
            lblViewRoom.Text = show.Room.RoomNumber.ToString();
            //lblViewDate.Text = show.Date.ToString();

            //cbShow.Text = show.Type.Title;
            //cbDate.Text = show.Date.Date.ToLongDateString();
            //cbTime.Text = show.Date.TimeOfDay.ToString();
            //lblLokale.Text = show.Room.RoomNumber.ToString();
            lblPrice1.Text = "DKR:  " + show.Prices[1].TicketPrice.ToString();
            lblPrice2.Text = "DKR:  " + show.Prices[0].TicketPrice.ToString();
            lblPrice3.Text = "DKR:  " + show.Prices[2].TicketPrice.ToString();

            //getAll();

        }

        public void date()
        {
            Show show = showService.GetShow(5);
            string time = show.Date.ToString();

            time = time.Substring(0, 10);

            lblViewDate.Text = time;
        }

        public void time()
        {
            Show show = showService.GetShow(5);
            string time = show.Date.ToString();

            time = time.Substring(2, 16);

            lblViewTime.Text = time;
        }

        public void getAll()
        {
            List<Show> shows = new List<Show>(showService.GetAllShows());

            for (int i = 0; i < shows.Count(); i++)
            {
                Show s = shows[i];
                //cbShow.Items.Add(" " + (i+1) + ": " + s.Type.Title.ToString());
                //cbShow.Click += new EventHandler(cbShow_SelectedIndexChanged);





            }
        }

        #region tickets

        public void populateCombobox()
        {
            string[] ad = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            cbAdult.Items.AddRange(ad);
            string[] ch = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            cbChild.Items.AddRange(ch);
            string[] se = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            cbSenior.Items.AddRange(se);

        }

        public void calculateTicketAmount()
        {

            if (total <= 9)
            {
                total = adultValue + childValue + seniorValue;

            }
            if (total >= 10)
            {
                MessageBox.Show("Det er kun tilladt at bestille max 9 billeter");
                total = 0;

                cbAdult.SelectedIndex = 0;
                cbChild.SelectedIndex = 0;
                cbSenior.SelectedIndex = 0;
            }

        }

        public void Adult()
        {
            if (cbAdult.SelectedIndex == 0)
            {
                adultValue = 0;

            }
            if (cbAdult.SelectedIndex == 1)
            {
                adultValue = 1;

            }
            if (cbAdult.SelectedIndex == 2)
            {
                adultValue = 2;

            }
            if (cbAdult.SelectedIndex == 3)
            {
                adultValue = 3;

            }
            if (cbAdult.SelectedIndex == 4)
            {
                adultValue = 4;

            }
            if (cbAdult.SelectedIndex == 5)
            {
                adultValue = 5;

            }
            if (cbAdult.SelectedIndex == 6)
            {
                adultValue = 6;

            }
            if (cbAdult.SelectedIndex == 7)
            {
                adultValue = 7;

            }
            if (cbAdult.SelectedIndex == 8)
            {
                adultValue = 8;

            }
            if (cbAdult.SelectedIndex == 9)
            {
                adultValue = 9;

            }




        }

        public void Child()
        {
            if (cbChild.SelectedIndex == 0)
            {
                childValue = 0;

            }
            if (cbChild.SelectedIndex == 1)
            {
                childValue = 1;

            }
            if (cbChild.SelectedIndex == 2)
            {
                childValue = 2;

            }
            if (cbChild.SelectedIndex == 3)
            {
                childValue = 3;

            }
            if (cbChild.SelectedIndex == 4)
            {
                childValue = 4;

            }
            if (cbChild.SelectedIndex == 5)
            {
                childValue = 5;

            }
            if (cbChild.SelectedIndex == 6)
            {
                childValue = 6;

            }
            if (cbChild.SelectedIndex == 7)
            {
                childValue = 7;

            }
            if (cbChild.SelectedIndex == 8)
            {
                childValue = 8;

            }
            if (cbChild.SelectedIndex == 9)
            {
                childValue = 9;

            }
        }

        public void Senior()
        {
            if (cbSenior.SelectedIndex == 0)
            {
                seniorValue = 0;

            }
            if (cbSenior.SelectedIndex == 1)
            {
                seniorValue = 1;

            }
            if (cbSenior.SelectedIndex == 2)
            {
                seniorValue = 2;

            }
            if (cbSenior.SelectedIndex == 3)
            {
                seniorValue = 3;

            }
            if (cbSenior.SelectedIndex == 4)
            {
                seniorValue = 4;

            }
            if (cbSenior.SelectedIndex == 5)
            {
                seniorValue = 5;

            }
            if (cbSenior.SelectedIndex == 6)
            {
                seniorValue = 6;

            }
            if (cbSenior.SelectedIndex == 7)
            {
                seniorValue = 7;

            }
            if (cbSenior.SelectedIndex == 8)
            {
                seniorValue = 8;

            }
            if (cbSenior.SelectedIndex == 9)
            {
                seniorValue = 9;
            }

        }

        private void cbAdult_SelectedIndexChanged(object sender, EventArgs e)
        {
            Adult();
            calculateTicketAmount();
            calculateAdultTicketPrice();

        }

        private void cbChild_SelectedIndexChanged(object sender, EventArgs e)
        {
            Child();
            calculateTicketAmount();
            calculateAdultTicketPrice();

        }

        private void cbSenior_SelectedIndexChanged(object sender, EventArgs e)
        {
            Senior();
            calculateTicketAmount();
            calculateAdultTicketPrice();

        }

        private void calculateAdultTicketPrice()
        {
            Show show = showService.GetShow(5);

            string adultprice = show.Prices[1].TicketPrice.ToString();
            string childprice = show.Prices[0].TicketPrice.ToString();
            string seniorprice = show.Prices[2].TicketPrice.ToString();
            test = (Convert.ToDouble(adultprice) * adultValue) + (Convert.ToDouble(childprice) * childValue) + (Convert.ToDouble(seniorprice) * seniorValue);
            lblTest.Text = "DKK: " + test.ToString();
        }

        #endregion

        #region Seats


        public void MakeButtons()
        {
            Point[] p = new Point[list.Count()];
            string log = "";
            totalseat = 0;
            itotalseat = 0;
            Button[] button = new Button[list.Count()];
            for (int i = 0; i < list.Count; i++)
            {

                button[i] = new Button();
                if (list[i].Status == "0")
                    button[i].BackColor = Color.Green;
                if (list[i].Status == "1")
                    button[i].BackColor = Color.Yellow;
                if (list[i].Status == "2")
                    button[i].BackColor = Color.Red;
                
                button[i].Height = 30;      //Størrelsen på knappen højde
                button[i].Width = 30;       //Størrelsen på knappen brede
                //button[i].Text = ((i + 1).ToString());    //Sætter sæde nummer
                button[i].Text = list[i].SeatId.ToString();
                button[i].Click += new EventHandler(button_Click);
                p[i] = new Point();

                seats.Add(button[i], list[i]);

                totalseat += list[i].SeatId;
                itotalseat += i;

                //p[i].X = i;

                //p[i].Y = 0;

                log += p.ToString() + "v\n";
                button[i].PointToClient(p[i]);
                button[i].Show();

            }
            double totalList = Math.Sqrt(list.Count);
            FlowLayoutPanel pan = new FlowLayoutPanel();
            pan.Width = Convert.ToInt32(totalList) * 40;       //Bestemmer størrelsen på panelet ( salen ) ved at bruge kvadratroden af det totale antal i arrayet for at få lavet salen kvadratisk.
            pan.Height = Convert.ToInt32(totalList) * 40;      //Samme som overstående. 
            pan.Controls.AddRange(button);                     //Tilføjer de antal knapper som indgår i arrayet.

            pnlSeat.Controls.Add(pan);                         // Tilføjer pan ( det panel som indeholder knapperne ) til pnlSeat som er panelet som er den max størrelse sal.

            //int bestSeat = totalseat / list.Count;
            //int mediItotal = itotalseat / list.Count;
            ////MessageBox.Show(" total : " + total.ToString() + " itotal :" + itotal.ToString() + " meditotal :" + mediItotal.ToString() + " bestseat :" + bestSeat.ToString());

            //button[mediItotal].BackColor = Color.Yellow;
            //seatService.LockSeat(bestSeat);

            #region algoritme

            //int m = totalseat / list.Count;               //Det midterste sæde
            //int tempM = m;                                //Midlertidig m
            //int mI = itotalseat / list.Count;             //Det midterste index
            //double aPerR = Math.Sqrt(list.Count);         //Antal per række
            //int rUp = mI - Convert.ToInt32(aPerR);       //1 række op
            //int rDown = mI + Convert.ToInt32(aPerR);       //1 række ned
            //int rI = 0;                                   //Højre sæde index
            //int lI = 0;                                   //Venstre sæde index

            //double k = aPerR / 2;
            //double rK = 0;
            //double vK = 0;
            //bool nextRow = false;
            //Seat s = new Seat();
            //s = seatService.GetSeat(m);

            //while (found == !true)
            //    nextRow = false;
            //while (s.Status == "1" || s.Status == "2" || nextRow == false)
            //{
            //    if (s.Status == "0")
            //    {
            //        seatService.LockSeat(m);
            //        booked_seats.Add(s);

            //        found = true;
            //    }
            //    else
            //    {
            //        rI++;
            //    }
            //    if ((k + rI) > aPerR)
            //    {
            //        tempM = m + rUp + lI;                        //Går en række op
            //        s = seatService.GetSeat(tempM);
            //        if (s.Status == "0")
            //        {
            //            seatService.LockSeat(tempM);
            //            booked_seats.Add(s);
            //            found = true;
            //        }
            //        else
            //        {
            //            tempM = m;                          //reset tempM
            //            tempM = m + rDown + lI;
            //            s = seatService.GetSeat(tempM);
            //            if (s.Status == "0")
            //            {
            //                seatService.LockSeat(tempM);
            //                booked_seats.Add(s);
            //                found = true;
            //            }
            //            else
            //            {
            //                tempM = m;
            //                tempM = m + rDown - lI;
            //                s = seatService.GetSeat(tempM);
            //                if (s.Status == "0")
            //                {
            //                    seatService.LockSeat(tempM);
            //                    booked_seats.Add(s);
            //                    found = true;
            //                }
            //            }
            //        }
            //        lI++;
            //        //nextRow = true;
            //    }
            //    tempM = m;
            //    tempM = m + rI;
            //    s = seatService.GetSeat(tempM);

            //    if (s.Status == "0")
            //    {
            //        seatService.LockSeat(tempM);
            //        booked_seats.Add(s);
            //        found = true;
            //    }
            //    else
            //    {
            //        tempM = m - rI;
            //    }

            //    if (k - rI <= 0)
            //    {
            //        tempM = m;
            //        tempM = m + rUp + lI;

            //        s = seatService.GetSeat(tempM);
            //        if (s.Status == "0")
            //        {
            //            seatService.LockSeat(tempM);
            //            booked_seats.Add(s);
            //            found = true;
            //        }
            //        else
            //        {
            //            tempM = m;                          //reset tempM
            //            tempM = m + rDown + lI;
            //            s = seatService.GetSeat(tempM);
            //            if (s.Status == "0")
            //            {
            //                seatService.LockSeat(tempM);
            //                booked_seats.Add(s);
            //                found = true;
            //            }

            //        }
            //        lI++;

            //        s = seatService.GetSeat(tempM);

            //        if (s.Status == "0")
            //        {
            //            seatService.LockSeat(tempM);
            //            booked_seats.Add(s);
            //            found = true;
            //        }
            //    }



            //}
            #endregion


        }
        
        void button_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            Seat s = seats[b];
            s = seatService.GetSeat(s.SeatId);
            //MessageBox.Show(s.Status);

            if (s.Status.Equals("0"))
            {
                seatService.LockSeat(s.SeatId, TEMP);
                b.BackColor = Color.Yellow;
                //booked_seats.Add(s);
            }
            if (s.Status.Equals("1"))
            {
                seatService.UnLockSeat(s.SeatId);
                b.BackColor = Color.Green;

                //booked_seats.Remove(s);
            }
            if (s.Status == "2")
                MessageBox.Show("Sædet er booked");           


        #endregion
        }

        private void SeatAlgorithm()
        {
            int m = totalseat / list.Count;               //Det midterste sæde
            int mI = itotalseat / list.Count;             //Det midterste index
            int rI = 0;                                   //Række index
            int Row = (int)Math.Sqrt(list.Count);           //Antal per række    
            int addRow = Convert.ToInt32(Row);            //En row converteret til int for man kan add den til tempM
            int cI = 0;                                   //Colum index

            int tempM = m;                                //Midlertidig m    
            bool fundet = false;
            double k = Row / 2;

            Seat s = new Seat();


            while (fundet == !true)
            {

                s = seatService.GetSeat(m);

                //if (k + rI > Row || k - rI < 0)
                //{
                    
                //    tempM = m;
                //    tempM += addRow;
                //    cI++;
                //    rI = 0;
                //}
                if (s.Status == "0")
                {
                    
                    seatService.LockSeat(m + rI, TEMP);
                    //Skal sættes ind i PartBooking.

                    fundet = true;
                }

                {
                    while (s.Status == "1" || s.Status == "2")
                    {
                        rI++;
                        if (k + rI > Row || k - rI < 0)
                        {


                            cI++;
                            rI = 0;
                        }
                        s = seatService.GetSeat(m + rI + (cI * Row));

                        if (s.Status == "0")
                        {
                            seatService.LockSeat(m + rI, TEMP);
                            //Skal sættes ind i PartBooking.

                            fundet = true;
                        }

                        if (s.Status == "1" || s.Status == "2")
                        {
                            s = seatService.GetSeat(m - rI + (cI * Row));

                            if (s.Status == "0")
                            {
                                seatService.LockSeat(m - rI + (cI * Row), TEMP);
                                //Skal sættes ind i PartBooking.

                                fundet = true;
                            }


                        }
                    }

                }
            }


        }
    }
}
